<?php

namespace App\Admin\Controllers;

use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use \App\Models\Divination;
use \App\Models\Category;
use Encore\Admin\Facades\Admin;
use App\Services\MakeProductThumbnail\MakeProductThumbnail;
use App\Admin\Actions\DivinationAction\MakeDeepThink;

class DivinationController extends AdminController
{
    protected $title = '山海归藏配置';
    
    protected function grid()
    {
        $grid = new Grid(new Divination());

	    $grid->disableExport();
	
	    $grid->column('id', 'ID');
	    $grid->column('title', '卜卦标题');
	    $grid->column('upper_num', '上挂数');
	    $grid->column('lower_num', '下挂数');
	  
	    $grid->column('image', '图片')->lightbox(['width' => 50, 'height' => 50]);
	   
	    $grid->column('created_at', '创建时间')->display(function () {
		    return $this->created_at->toDatetimeString();
	    });
	
	    $grid->disableRowSelector();
	    $this->gridActions($grid);
        return $grid;
    }

     protected function gridActions(Grid $grid)
    {
	    $grid->actions(function (Grid\Displayers\Actions $actions) {
	    	$actions->disableDelete();

	    	if (in_array(Admin::user()->id, [1,2])) {
		    	$actions->add(new MakeDeepThink());

 			}
		    
	    });
    }
	
	 protected function detail($id)
    {
        $show = new Show(Divination::findOrFail($id));
        return $show;
    }
	
    protected function form()
    {
        $form = new Form(new Divination());

	    $form->text('title', '卜卦标题')->rules('required');
	    $form->number('upper_num', '上挂数')->rules('required');
	    $form->number('lower_num', '下挂数')->rules('required');
	    $form->text('yao', '爻')->rules('required');
	     $form->hasMany('yaoDivinations', "变爻", function ($form)  {
			    /*$form->select('category_id','分类')->options(Category::all()->pluck('name', 'id'));
    	 		$form->textarea('content', '深度解析')->rules('required');*/

    	 		$form->select('by_dividend_id','变爻名称')->options(Divination::all()->pluck('title', 'id'));
    	 		$form->number('yao_count', '变爻数')->default(0);
    	 });

	    $form->image('image', '图片')->uniqueName()->help('NOTE：保证清晰的前提下尽可能压缩体积');
	    $form->textarea('content', '解挂内容')->rules('required');
	    $form->textarea('words', '卦辞意义解读')->rules('required');
	    $form->textarea('essential', '本卦吉运要义')->rules('required');
	    $form->textarea('chongjie', '山海易象冲解')->rules('required');
	    
	    $this->formSaved($form);
        return $form;
    }

    protected function formSaved(Form $form)
	{
		$form->saved(function () use ($form) {
			
			
			app(MakeProductThumbnail::class, ['divination' => $form->model()])->make();
		});
	}
}
